package com.liuwa.common.core.service.impl;

import com.liuwa.common.core.dao.SortableCurdDao;
import com.liuwa.common.core.domain.SortableEntity;
import com.liuwa.common.core.service.SortableCurdService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;

/**
 * @author: Liuwa
 * @Date: 2023-10-27
 * @Description: 可排序 CURD Service
 */
public abstract class SortableCurdServiceImpl<Pk, D extends SortableCurdDao<Pk, T>, T extends SortableEntity> extends CurdServiceImpl<Pk, D, T> implements SortableCurdService<Pk, D, T> {

    @Autowired
    private D  sortableCurdDao;


    @Override
    public T insert(T entity) {
        // 自动设置排序
        if(entity.getDisplayIndex() == null){
            entity.setDisplayIndex(getMaxDisplayIndex() + 1);
        }
        return super.insert(entity);
    }

    @Override
    public int getMaxDisplayIndex() {
        return sortableCurdDao.getMaxDisplayIndex();
    }
}
